﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Configuration;
using System.Web;
using DTO;
using Moved;

namespace BUS
{
    public class AccountBUS
    {
        private static String _connString = "";

        static AccountBUS()
        {
            _connString = ConfigurationManager.ConnectionStrings["WebChamCongConnectionString"].ConnectionString.ToString();
        }
        public static string catchuoi(string str)
        {
            string[] kt = str.Split('&');
            string bp = "";
            for (int i = 0; i < kt.Count(); i++)
            {
                bp += kt[i] + "&";
            }
            return bp;
        }
        public static string MaNgauNhien_SoChu(int codeCount)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
            string[] allCharArray = allChar.Split(',');
            string randomCode = "";
            int temp = -1;

            Random rand = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(36);
                if (temp != -1 && temp == t)
                {
                    return MaNgauNhien_SoChu(codeCount);
                }
                temp = t;
                randomCode += allCharArray[t];
            }
            return randomCode;
        }

        public static string MaHoaMD5(string sinput)
        {
            // chuyển chuỗi ký tự thành mảng số nguyên
            byte[] bytes = Encoding.UTF8.GetBytes(sinput);
            // tạo lớp MD5 của NET
            MD5 md5 = new MD5CryptoServiceProvider();
            // thực thi phần mã hóa => mảng byte đã băm
            byte[] hash = md5.ComputeHash(bytes);
            // chuyển thành chuỗi ký tự
            StringBuilder sb = new StringBuilder();
            foreach (byte b in hash)
            {
                sb.Append(string.Format("{0:X2}", b));
            }
            return sb.ToString();
        }
        public static DTO.AccountDTO getForgetPassword(string name, string ACID,string Mail)
        {
                string localMail = Mail;
                string localName = name;
                string localAcID = ACID;
                DTO.AccountDTO kq = null;
                using(Moved.Moved_DB dc=new Moved_DB(_connString ))
                {
                    var listInfo = (from usinfo in dc.AL_UserInfo
                                  join ac in dc.WIT_User on usinfo.UserID equals ac.UserID
                                      where usinfo.Name==localName 
                                      && usinfo.ACNo==localAcID
                                      && usinfo.Email==localMail
                                      select new
                                      {
                                         FullName=usinfo.FullName,
                                         id=ac.UserID
                                      }
                                      ).Distinct();
                    foreach (var objInfo in listInfo)
                    {
                        kq = new AccountDTO();
                        kq.Fullname=objInfo.FullName;
                        kq.Id = objInfo.id;
                    }
                    return kq;
                }


        }
        public static DTO.AccountDTO getInfoByID(int id)
        {
         
                int localID = id;
                DTO.AccountDTO kq = null;
                using (Moved.Moved_DB dc = new Moved_DB(_connString))
                {
                    var objID = (from Info in dc.AL_UserInfo
                                 where Info.UserID == localID
                                 select new 
                                 {
                                     mail=Info.Email
                                 }
                                 ).Distinct();
                    foreach (var ob in objID)
                    {
                        kq = new AccountDTO();
                        kq.Email = ob.mail;
                    }
                    return kq;
                }
          
        }
        public static bool createAccLogin(Int32 UserID, String Password, Int32 State)
        {
            try 
            {
                using (Moved.Moved_DB dc = new Moved_DB(_connString))
                {

                    WIT_User us = new WIT_User();
                    us.UserID = UserID;
                    us.Password = Password;
                    us.State = State;
                    dc.WIT_User.InsertOnSubmit(us);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }        
        }

        public static bool updateInfoAcc(Int32 UserID, String Password)
        {
            bool kq = false;
            try
            {
                using (Moved.Moved_DB dc = new Moved_DB(_connString))
                {
                    var ListAcc = (from ac in dc.WIT_User
                                   where ac.UserID == UserID
                                   select ac
                                     ).SingleOrDefault();
                    if (ListAcc != null)
                    {
                        ListAcc.Password = Password;
                        dc.SubmitChanges();
                        kq = true;
                    }

                }
            }
            catch (Exception e)
            {
                kq = false;
                throw e;
            }
            return kq;
        }

        public static bool setAccStatus(int userID, int userState)
        {
            bool status = false;
            int localUserID = userID;
            int localUserState = userState;
            try
            {
                using (Moved.Moved_DB dc = new Moved_DB(_connString))
                {
                    var listAcc = (from ac in dc.WIT_User
                                   where ac.UserID == localUserID
                                   select ac
                                        ).SingleOrDefault();
                    if (listAcc != null)
                    {
                        listAcc.State = localUserState;
                        dc.SubmitChanges();
                        status = true;
                    }
                }
            }
            catch { status = false; }
           
            return status;
        }
        public static DTO.AccountDTO getAccloginadmin(int id, string Password)
        {
            int UserIDLocal = id;
            string PassLocal = Password;
            DTO.AccountDTO user = null;
            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                var objCanv = (from us in dc.WIT_User
                               where us.Password == PassLocal
                                   && us.UserID==UserIDLocal
                               select new
                               {
                                   rule = us.State,
                                   idUser = us.UserID,
                               });
                foreach (var item in objCanv)
                {
                    user = new AccountDTO();
                    user.Id = item.idUser;
                    user.State = item.rule;
                    break;

                }
                return user;
            }

        }
        public static DTO.AccountDTO getAcclogin(String Fullname, String Password)
        {
            string UserNameLocal = Fullname;
            string PassLocal = Password;
            DTO.AccountDTO user = null;
            using (Moved.Moved_DB dc = new Moved_DB(_connString))
            {
                var objCanv = (from us in dc.WIT_User
                               join info in dc.AL_UserInfo on us.UserID equals info.UserID
                               where us.Password == PassLocal
                                   && string.Compare(info.Name, UserNameLocal) == 0
                               select new
                               {
                                   namebp = info.FullName,
                                   rule = us.State,
                                   idUser = us.UserID,
                                   name  = info.Name,
                                   departmentID=info.DepartmentID

                               });
                foreach (var item in objCanv)
                {
                    user = new AccountDTO();
                    user.Fullname = item.namebp;
                    user.Id = item.idUser;
                    user.State = item.rule;
                    user.Name = item.name;
                    user.DepartmentID = (int)item.departmentID;
                    break;

                }
                return user;
            }

        }
    }
}
